package com.yao.component.circle;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.yao.component.circle.dataobject.Post;
import com.yao.component.circle.dataobject.CircleProfile;
import com.yao.component.circle.dataobject.Message;
import com.yao.sdk.net.NetWorkListener;
import com.yao.sdk.net.ResponseInfo;
import com.yao.sdk.net.volley.LruImageCache;

import java.util.List;

/**
 * Created by Administrator on 2015/10/26.
 */
public class CircleView extends RelativeLayout implements View.OnClickListener{

    private RecyclerView recyclerView;
    private CircleAdapter circleAdapter;
    private RecyclerView.LayoutManager layoutManager;
    private CircleDataSource dataSource;
    private CircleStyle circleStyle;

    private PopupWindow editWindow;// 回复window
    private EditText editReply;  //评论编辑框
    private Button btnSend;      // 发送评论按钮
    private int delMode = MODE_DELETE_MESSAGE;
    public static int MODE_DELETE_POST = 1;
    public static int MODE_DELETE_MESSAGE = 2;

    private PopupWindow commentDelWindow;// 删除评论window
    private Button btnDelComment;  //删除评论按钮
    private Button btnNotDelComment; //取消删除评论按钮
    private RelativeLayout delCommentWrapper;
    RequestQueue mQueue = null;
    ImageLoader imageLoader = null;
    AbstractCircleDataSource defaultCircleDataSource;

    public CircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AbstractCircleDataSource getDefaultCircleDataSource() {
        if (defaultCircleDataSource == null) {
            defaultCircleDataSource = new DefaultCircleDataSourceImpl();
            defaultCircleDataSource.setCircleView(this);
        }
        return defaultCircleDataSource;
    }

    private void init(Context context) {
        circleStyle = new DefaultCircleStyleImpl();
        initRecyclerView(context);
        initVolley(context);
    }

    /**
     *  用于下载缓存图片
     */
    private void initVolley(Context context) {
        mQueue = Volley.newRequestQueue(context);
        imageLoader = new ImageLoader(mQueue, LruImageCache.instance());

    }

    private void initRecyclerView(Context context) {

        recyclerView = new RecyclerView(context);
        // use a linear item_circle manager
        recyclerView.setHasFixedSize(false);
        layoutManager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        addView(recyclerView);
    }

    private void initCommentDelWindow(Context context) {
        LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.pop_del_comment, null);

        commentDelWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        commentDelWindow.setFocusable(true);
        commentDelWindow.setOutsideTouchable(true);  //设置点击屏幕其它地方弹出框消失
        commentDelWindow.setBackgroundDrawable(new BitmapDrawable());

        btnDelComment = (Button) view.findViewById(R.id.del_comment_btn);
        btnNotDelComment = (Button) view.findViewById(R.id.not_del_comment_btn);
        delCommentWrapper = (RelativeLayout) view.findViewById(R.id.del_comment_wrapper);
        btnDelComment.setOnClickListener(this);
        btnNotDelComment.setOnClickListener(this);
        delCommentWrapper.setOnClickListener(this);

    }

    public void showCommentDelWindow(int delMode) {
        this.delMode = delMode;
        if (commentDelWindow == null) {
            initCommentDelWindow(getContext());
        }
        commentDelWindow.showAtLocation(this, Gravity.BOTTOM, 0, 0);
    }

    public void dismissCommentDelWindow() {
        if (commentDelWindow != null) {
            commentDelWindow.dismiss();
        }
    }

    private void initEditWindow(Context context) {
        LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.pop_reply, null);

        editWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        editWindow.setFocusable(true);
        editWindow.setOutsideTouchable(true);  //设置点击屏幕其它地方弹出框消失
        editWindow.setBackgroundDrawable(new BitmapDrawable());

        editReply = (EditText) view.findViewById(R.id.edit_reply);
        btnSend = (Button) view.findViewById(R.id.btn_send);
        btnSend.setOnClickListener(this);
        editReply.setFocusable(true);
        editReply.requestFocus();
        // 以下两句不能颠倒
        editWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        editWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

    }

    public void showEditWindow(String hint) {
        if (editWindow == null) {
            initEditWindow(getContext());
        }
        editReply.setText("");
        editReply.setHint(hint);
        editWindow.showAtLocation(this, Gravity.BOTTOM, 0, 0);
    }

    public void dismissEditWindow() {
        if (editWindow != null) {
            editWindow.dismiss();
        }
    }



    public void setDataSource(CircleDataSource dataSource) {
        this.dataSource = dataSource;
    }



    public CircleDataSource getDataSource() {
        return this.dataSource;
    }

    public CircleStyle getCircleStyle() {
        return circleStyle;
    }

    public void requestPost() {
        if (dataSource != null) {
            dataSource.requestGetPost();
        }
    }

    // 刷新朋友圈内容
    public void refresh() {
        if (circleAdapter == null) {
            circleAdapter = new CircleAdapter();
            circleAdapter.setCircleView(this);
            circleAdapter.setImageLoader(imageLoader);

            circleAdapter.setCircleStyle(circleStyle);
            recyclerView.setAdapter(circleAdapter);
        }
        circleAdapter.setDataSource(dataSource);
        circleAdapter.notifyDataSetChanged();
    }

    // 刷新朋友圈内容单行
    public void refreshItem(int index) {
//        if (circleAdapter == null) {
//            circleAdapter = new CircleAdapter();
//            circleAdapter.setCircleView(this);
//            circleAdapter.setImageLoader(imageLoader);
//
//            circleAdapter.setCircleStyle(circleStyle);
//            recyclerView.setAdapter(circleAdapter);
//        }
//        circleAdapter.setDataSource(dataSource);
        circleAdapter.notifyItemChanged(index);
    }

    @Override
    public void onClick(View v) {
        if (v == btnSend) {
            addMessage();
        } else if (v == btnDelComment) {
            if (delMode == MODE_DELETE_POST) {
                delPost();
            } else if (delMode == MODE_DELETE_MESSAGE) {
                delMessage();
            }
        } else if (v == btnNotDelComment) {
            dismissCommentDelWindow();
        } else if (v == delCommentWrapper) {
            dismissCommentDelWindow();
        }
    }

    // 添加一个新回复或者评论
    public void addMessage() {
        Post post = dataSource.getCachedPost().get(MessageEditor.getInstance().getCircleItemIndex());
        CircleProfile circleProfile = dataSource.getProfile();
        Message newMessage = new Message();
        if (MessageEditor.getInstance().getMessageType() == Message.TYPE_REPLY) {
            Message message = post.getMessageList().get(MessageEditor.getInstance().getMessageItemIndex());
            newMessage.setMessageType(Message.TYPE_REPLY);
            newMessage.setPostId(post.getId());
            newMessage.setToCircleUserName(message.getFromCircleUserName());
            newMessage.setToCircleUserId(message.getFromCircleUserId());
            newMessage.setFromCircleUserName(circleProfile.getName());
            newMessage.setFromCircleUserId(circleProfile.getUserId());
            newMessage.setContent(editReply.getText().toString());

        } else if (MessageEditor.getInstance().getMessageType() == Message.TYPE_COMMENT) {
            newMessage.setMessageType(Message.TYPE_COMMENT);
            newMessage.setToCircleUserName(post.getPosterName());
            newMessage.setToCircleUserId(post.getUserId());
            newMessage.setPostId(post.getId());
            newMessage.setFromCircleUserName(circleProfile.getName());
            newMessage.setFromCircleUserId(circleProfile.getUserId());
            newMessage.setContent(editReply.getText().toString());
        }
        post.getMessageList().add(newMessage);

       // circleAdapter.notifyDataSetChanged();
        if (dataSource != null) {
            dataSource.requestPublishMessage(newMessage);
        }
        dismissEditWindow();
    }

    // 删除一个评论或者回复
    public void delMessage() {
        Post post = dataSource.getCachedPost().get(MessageEditor.getInstance().getCircleItemIndex());
        Message message = post.getMessageList().get(MessageEditor.getInstance().getMessageItemIndex());
        post.getMessageList().remove(message);
       // circleAdapter.notifyDataSetChanged();
        if (dataSource != null) {
            dataSource.requestDeleteMessage(message.getId());
        }
        dismissCommentDelWindow();
    }

    //删除一个圈子动态
    public void delPost() {
        Post post = dataSource.getCachedPost().get(MessageEditor.getInstance().getCircleItemIndex());
        if (dataSource != null) {
            dataSource.requestDeletePost(post.getId());
        }
        dismissCommentDelWindow();
    }

}
